/* eslint-disable @typescript-eslint/no-unused-vars */
import React from 'react';
import { BottomAction } from '@/components';
import { Button, message, Form, Popconfirm, Empty } from 'antd';
import pageTabUtil from '@/utils/pageTabUtil';
import { useFormChange } from '@/pages/RecruitEmail/hooks';
import { history } from 'umi'
import { CollapseHeader, LimitInput, AccountTable } from '../components';
import styles from './index.less';
import { completeHandover } from '../../service';
import { LEAVE_STATUS_OBJ } from '../../dictionary';

function ITHandover({ form, data = [], dimissionOrderId, ...rest }) {
    const [submitLoading, setSubmitLoading] = React.useState();
    const [operateChangeFlag, setOperateChangeFlag] = React.useState(false);
    const { hasFormChange, initialForm } = useFormChange(form);
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const { getFieldDecorator, validateFields } = form;
    const dataSource = React.useMemo(() => {
        const { matterDetail } = data;
        const handlerDetail = matterDetail?.find(m => m.handoverType === LEAVE_STATUS_OBJ.IT_HANDOVER);
        initialForm();
        if (handlerDetail) {
            return handlerDetail?.handoverAccount;
        }
        return [];
    }, [data]);
    const handleSubmit = () => {
        const { matterDetail } = data;
        const handlerDetail = matterDetail?.find(m => m.handoverType === LEAVE_STATUS_OBJ.IT_HANDOVER);
        if (!handlerDetail) {
            message.error('未查询到IT交接详情');
            return;
        }
        validateFields(async (err, values) => {
            console.log('it交接数据', err, values);
            if (err) {
                return;
            }
            const { handoverAccount = [] } = values;
            if (handoverAccount.some(item => item.status !== 2)) {
                message.warning('账号关闭未完成，已保存当前进度，可再次点击完成操作');
                return;
            }
            try {
                setSubmitLoading(true);
                const { code, msg } = await completeHandover({
                    id: handlerDetail?.id,
                    dimissionOrderId,
                    handoverType: LEAVE_STATUS_OBJ.IT_HANDOVER,
                    ...values
                });
                if (code === 10000) {
                    message.success('提交成功！');
                    history.replace('/office/personnelManagement/onboardAndLeave/leaveManagement/list');
                } else {
                    message.error(msg);
                }
                setSubmitLoading(false);
            } catch (e) {
                setSubmitLoading(false);
            }
        });
    };
    const fullLineLayout = {
        labelCol: { span: 2 },
        wrapperCol: { span: 14 }
    };
    return (
        <Form layout="horizontal" {...fullLineLayout}>
            <CollapseHeader title="资产回收">
                <div style={{ margin: '40px 0px 60px' }}>
                    <Empty description="开发中，敬请期待" />
                </div>
            </CollapseHeader>
            <CollapseHeader title="关闭账号">
                {getFieldDecorator('handoverAccount', {
                    initialValue: dataSource
                })(<AccountTable setOperateChangeFlag={setOperateChangeFlag} />)}
            </CollapseHeader>
            <CollapseHeader title="备注" style={{ width: 500 }}>
                {getFieldDecorator('remark', {
                    initialValue: data.matterDetail[0].remark
                })(<LimitInput area autoSize={{ minRows: 3, maxRows: 6 }} placeholder="请填写备注" maxLength={4000} />)}
            </CollapseHeader>
            <BottomAction>
                {hasFormChange() || operateChangeFlag ? (
                    <Popconfirm
                        title={
                            <>
                                当前页面存在改动信息暂未提交，
                                <br />
                                确定离开此页面？
                            </>
                        }
                        arrowPointAtCenter
                        onConfirm={() => pageTabUtil.goBack()}
                        okText="确定"
                        cancelText="取消"
                    >
                        <Button style={{ marginRight: 8 }}>取消</Button>
                    </Popconfirm>
                ) : (
                    <Button onClick={() => pageTabUtil.goBack()}>取消</Button>
                )}
                <Button type="primary" onClick={handleSubmit} loading={submitLoading}>
                    确定
                </Button>
            </BottomAction>
        </Form>
    );
}
export default Form.create()(ITHandover);
